package com.budwk.app.oss.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.app.oss.models.SysOss;
import com.budwk.app.oss.services.SysOssService;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.log.annotation.SLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@IocBean
@At("/platform/oss/oss")
@ApiDefinition(tag = "SysOss")
@Slf4j
public class SysOssController {
    @Inject
    private SysOssService sysOssService;

    @At("/upload")
    @Ok("json")
    @POST
    @ApiOperation(name = "单个文件上传")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "file", example = "1", description = "文件")
            },
            implementation = SysOss.class
    )
    @ApiResponses(
            implementation = SysOss.class
    )
    @AdaptBy(type = UploadAdaptor.class)
    @SaCheckLogin
    public Result<?> upload(@Param("file") TempFile file) {
        return Result.data(sysOssService.upload(file));
    }

    @At("/uploads")
    @Ok("json")
    @POST
    @ApiOperation(name = "多个文件上传")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "files", example = "1", description = "文件")
            },
            implementation = SysOss.class
    )
    @ApiResponses(
            implementation = SysOss.class
    )
    @AdaptBy(type = UploadAdaptor.class)
    public Result<?> uploads(@Param("files") List<TempFile> files) {
        Map<String, Object> ajax = new HashMap<>();
        List<String> urls = new ArrayList<String>();
        List<String> fileNames = new ArrayList<String>();
        List<String> ossIdList = new ArrayList<String>();
        for (TempFile file : files) {
            SysOss sysOss = sysOssService.upload(file);
            int index = sysOss.getUrl().lastIndexOf("443");
            String url = sysOss.getUrl().substring(index+3,sysOss.getUrl().length() );
            urls.add( url);
            fileNames.add(sysOss.getOriginalName());
            ossIdList.add(sysOss.getOssId().toString());
        }
        ajax.put("urls", StringUtils.join(urls, ","));
        ajax.put("fileNames", StringUtils.join(fileNames, ","));
        ajax.put("ossIdList", StringUtils.join(ossIdList, ","));
        return Result.success(ajax);
    }


    @At("/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询SysOss")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = SysOss.class
    )
    @ApiResponses(
    implementation = SysOss.class
    )
    @SaCheckLogin
    public Result<?> list(@Param("..")SysOss bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        
        if (bean.getFileName() !=null && Strings.isNotBlank(bean.getFileName().toString())) {
            cnd.and("fileName","=",bean.getFileName());
        }
        
        return Result.data(sysOssService.listPage(pageNo, pageSize, cnd));
    }

    @At("/get")
    @Ok("json")
    @GET
    @ApiOperation(name = "获取SysOss")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckLogin
    public Result<?> getData(@Param("id")Long id, HttpServletRequest req) {
        SysOss sysOss = sysOssService.fetch(id);
        if (sysOss == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(sysOss);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除SysOss")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除SysOss", value = "${args[0]}")
    @SaCheckPermission("oss.oss.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        SysOss sysOss = sysOssService.fetch(id);
        if (sysOss == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        sysOssService.delete(id);
        return Result.success();
    }
}
